// pages/index/index.js
const util = require('../../utils/util.js')
const BL = require('../../utils/lanya.js')
var pos = require('../../utils/pos.js')
var GXZT;//更新状态
var that;
const app = getApp();
import utils from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navTop: '',
    navHeight: '',
    isEwm:false,
    info:{},
    object:{},
    storeList:[],
    orderList:[],
    LYData: {
      openOK: false,//蓝牙是否打开
      BLZT: false,//蓝牙状态是否可用
      BLSO: false,//蓝牙是否正在搜索
      TS: ""      
    },
    LYSB: [],
    placeName:'',
    bankList:[],
    startTime:'',
    endTime:'',
    endDate:'',
    player:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let capsuleInfo = utils.capsuleData()
    utils.locationData()
    setTimeout(() => {
      this.locationData()
    }, 500);
    this.setData({
      navTop: capsuleInfo.marginTop,
      navHeight: capsuleInfo.height,
    })
    var currentDate= new Date()
    var ymd=util.formatDateymd(currentDate)
    this.setData({
      startTime:ymd,
      endTime:ymd,
      endDate:ymd
    })
  },
  closeEwm(){
    this.setData({
      isEwm:false
    })
  },
  openEwm(){
    this.setData({
      isEwm:true
    })
  },
  // 显示更多订单
  gotoOrder(){
    wx.navigateTo({
      url: '/pages/oredrList/index',
    })
  },
  // 团购套餐
  gotoPackage(){
    wx.navigateTo({
      url: '/pages/productManagement/index',
    })
  },
  // 去产品库
  gotoProductList(){
    wx.navigateTo({
      url: '/pages/productList/index',
    })
  },
  // 去供应
  gotoGyList(){
    wx.navigateTo({
      url: '/pages/gyList/index',
    })
  },
  //去桌号
  gotoNumber(){
    wx.navigateTo({
      url: '/pages/tableNumber/index',
    })
  },
  gotoDetail(){
    wx.navigateTo({
      url: '/pages/numberDetail/index',
    })
  },
  gotomerchantGuidelines(){
    wx.navigateTo({
      url: '/pages/merchantGuidelines/index',
    })
  },
  gotoVideo(){
    wx.navigateTo({
      url: '/pages/videoList/index',
    })
  },
  storeHome(){
    let data = {
      id:wx.getStorageSync('userInfo').storeId
    }
    app.agriknow.storeHome(data).then(res=>{
      console.log(res)
      var datas=res.data
      this.setData({
        info:datas
      })

      if(datas.openTime){
        this.setData({
          openTime:datas.openTime
        })
      }
      if(datas.closeTime){
        this.setData({
          closeTime:datas.closeTime
        })
      }

      if(!datas.isApply){
        console.log('未进件')
        wx.reLaunch({
          url: '/pages/wode/merchant',
        })
      }
    })
  },
  storeHomeDate(){
    let data = {
      storeId:wx.getStorageSync('userInfo').storeId
    }
    data.startTime=this.data.startTime
    data.endTime=this.data.endTime
    console.log(util.isValidDateRange(data.startTime,data.endTime))
    if(!util.isValidDateRange(data.startTime,data.endTime)){
      wx.showToast({
        title: '结束日期不能小于开始日期',
        icon:'none',
        duration:1500,
        mask:true
      })
      return false
    }else if(util.isValidDateRangeDay(data.startTime,data.endTime)>90){
      wx.showToast({
        title: '查询日期不能超出90天',
        icon:'none',
        duration:1500,
        mask:true
      })
      return false
    }
    app.agriknow.storeHomeDate(data).then(res=>{
      console.log(res)
      this.setData({
        object:res.data
      })
    })
  },
  storeDepotPage(){
    let data = {
      id:wx.getStorageSync('userInfo').storeId,
      page:1,
      limit:2
    }
    app.agriknow.storeDepotPage(data).then(res=>{
      console.log(res)
      this.setData({
        storeList:res.data.list
      })
    })
  },
  storeOrderPage(){
    let data = {
      storeId:wx.getStorageSync('userInfo').storeId,
      type:1,
      page:1,
      limit:2
    }
    app.agriknow.storeOrderPage(data).then(res=>{
      console.log(res)
      this.setData({
        orderList:res.data.list
      })
    })
    console.log(this.data.orderList)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },
   //获取位置
   locationData() {
    //获取当前位置
    // utils.locationData()
    let locationInfo = wx.getStorageSync('locationInfo')
    if(locationInfo){
      this.setData({
        latitude: locationInfo.location.lat,
        longitude: locationInfo.location.lng,
        placeName: locationInfo.address_component.city,
      })
      console.log(this.data.placeName)
    }
    else{
    }
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var userInfo=wx.getStorageSync('userInfo')
    if(userInfo.storeId){
      this.storeHome()
      this.storeHomeDate()
      this.storeDepotPage()
      this.storeOrderPage()

     
      
    }else{
      wx.reLaunch({
        url: '/pages/login/index',
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  datePicker:function(e){
    var name=e.currentTarget.dataset.name
    this.setData({
      [name]:e.detail.value
    })
  },
  setStatus:function(){
    var that=this
    var info=this.data.info
    var data={}
    data.id=wx.getStorageSync('userInfo').storeId
    app.agriknow.setStatus(data).then(res=>{
      console.log(res)
      if(res.code=='000000'){
        if(info.isRest==0){
          that.setData({
            'info.isRest':1
          })
        }else{
          that.setData({
            'info.isRest':0
          })
        }
      }
    })
  },
  toDetail:function(e){
    var id=e.currentTarget.dataset.id
    var type=e.currentTarget.dataset.type
    wx.navigateTo({
      url: '/pages/orderDetail/index?id='+id+'&type='+type,
    })
  },
  openPlayer:function(){
    this.setData({
      player:1
    })
  },
  closePlayer:function(){
    this.setData({
      player:0
    })
  },
  setHours:function(){
    var data={}
    data.storeId=wx.getStorageSync('userInfo').storeId
    data.openTime=this.data.openTime
    data.closeTime=this.data.closeTime
    app.agriknow.commRequest('ht/store/updTradeTime',data).then(res=>{
      console.log(res)
      this.storeHome()
      this.closePlayer()
    })
  },
  
  toVideo:function(e){
    var item=e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/videoList/detail?item='+encodeURIComponent(JSON.stringify(item)),
    })
  }
})